﻿using BusinessCache;
using BusinessDB;
using BusinessService.proto;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessService
{
    public class OriginalBusiness
    {
        public byte[] GetStaticResource()
        {
            byte[] result = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "ResItemDB";
            if (memcacheFacade.Contains(key))
            {
                result = (byte[])memcacheFacade[key];
            }
            else
            {
                ResourceService rs = new ResourceService();

                ResArray.Builder builder = new ResArray.Builder();
                foreach (ResItemDB itemDB in rs.GetItems())
                {
                    ResItem.Builder itemBuilder = new ResItem.Builder();
                    itemBuilder.SetId(itemDB.TeamID);
                    itemBuilder.SetItemID(itemDB.ItemID);
                    itemBuilder.SetItemName(itemDB.ItemName);
                    builder.ResList.Add(itemBuilder.Build());
                }

                result = builder.Build().ToByteArray();
                memcacheFacade.Add(key, result, DateTime.Now.AddDays(2));
            }

            return result;

        }

        public string SearchProdName()
        {
            MemcacheFacade memcacheFacade = new MemcacheFacade();
            ProdDB[] prods = null;
            string key = "ProdDB";
            if (memcacheFacade.Contains(key))
            {
                prods = (ProdDB[])memcacheFacade[key];
            }
            else
            {
                ProdService ps = new ProdService();
                prods = ps.GetItems().ToArray();
                memcacheFacade.Add(key, prods, DateTime.Now.AddDays(2));
            }

            StringBuilder sb = new StringBuilder();
            var query = (from r in prods select r.ProdLabel).Distinct();
            foreach (string s in query)
            {
                sb.AppendFormat("{0},", s);
            }

            if (sb.Length > 0)
                sb.Remove(sb.Length - 1, 1);


            return sb.ToString();
        }

        public int AddProd(ProdDB prod)
        {
            ProdDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "ProdDB";

            ProdService ps = new ProdService();
            if (memcacheFacade.Contains(key))
            {
                ps.Add(prod);
                array = (ProdDB[])memcacheFacade[key];

                List<ProdDB> list = new List<ProdDB>();
                list.AddRange(array);
                list.Add(prod);
                memcacheFacade.Set(key, list.ToArray(), DateTime.Now.AddDays(2));
            }
            else
            {
                ps.Add(prod);
                array = ps.GetItems().ToArray();
                memcacheFacade.Add(key, array, DateTime.Now.AddDays(2));
            }

            return prod.Id.Pid;
        }

        public ProdDB GetProd(short id)
        {
            MemcacheFacade memcacheFacade = new MemcacheFacade();

            ProdDB[] array = null;
            string key = "ProdDB";

            ProdService ps = new ProdService();
            if (!memcacheFacade.Contains(key))
            {
                array = ps.GetItems().ToArray();
                memcacheFacade.Add(key, array, DateTime.Now.AddDays(2));
            }
            else
            {
                array = (ProdDB[])memcacheFacade[key];
            }

            return (from r in array where r.Id.Pid == id select r).FirstOrDefault();
        }

        public bool UpdateProd(ProdDB prod)
        {
            ProdDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "ProdDB";
            ProdService ps = new ProdService();
            if (memcacheFacade.Contains(key))
            {
                ps.Update(prod);

                array = (ProdDB[])memcacheFacade[key];

                var item = (from r in array
                            where r.Id.Pid == prod.Id.Pid
                            select r).FirstOrDefault();

                if (item != null)
                {
                    List<ProdDB> list = new List<ProdDB>();
                    list.AddRange(array);
                    list.Remove(item);
                    list.Add(prod);

                    memcacheFacade.Set(key, list.ToArray(), DateTime.Now.AddDays(2));
                }
            }
            else
            {
                ps.Update(prod);
                memcacheFacade.Add(key, ps.GetItems().ToArray(), DateTime.Now.AddDays(2));
            }

            return true;
        }

        public bool DeleteProd(short id)
        {
            ProdDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "ProdDB";
            ProdService ps = new ProdService();
            if (memcacheFacade.Contains(key))
            {
                ps.Delete(id);

                array = (ProdDB[])memcacheFacade[key];

                var item = (from r in array
                            where r.Id.Pid == id
                            select r).FirstOrDefault();

                if (item != null)
                {
                    List<ProdDB> list = new List<ProdDB>();
                    list.AddRange(array);
                    list.Remove(item);

                    memcacheFacade.Set(key, list.ToArray(), DateTime.Now.AddDays(2));
                }
            }
            else
            {
                ps.Delete(id);
                memcacheFacade.Add(key, ps.GetItems().ToArray(), DateTime.Now.AddDays(2));
            }

            return true;
        }

        public UserDB[] GetUsers()
        {
            UserDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "UserDB";
            UserService us = new UserService();
            if (memcacheFacade.Contains(key))
            {
                array = (UserDB[])memcacheFacade[key];
            }
            else
            {
                array = us.GetItems().ToArray();
                memcacheFacade.Add(key, array, DateTime.Now.AddDays(2));
            }

            return array;
        }

        public int AddUser(UserDB user)
        {
            UserDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "UserDB";

            UserService us = new UserService();
            if (memcacheFacade.Contains(key))
            {
                us.Add(user);
                array = (UserDB[])memcacheFacade[key];

                List<UserDB> list = new List<UserDB>();
                list.AddRange(array);
                list.Add(user);
                memcacheFacade.Set(key, list.ToArray(), DateTime.Now.AddDays(2));
            }
            else
            {
                us.Add(user);
                array = us.GetItems().ToArray();
                memcacheFacade.Add(key, array, DateTime.Now.AddDays(2));
            }

            return user.Id.Pid;
        }

        public bool UpdateUser(UserDB User)
        {
            UserDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "UserDB";
            UserService us = new UserService();
            if (memcacheFacade.Contains(key))
            {
                us.Update(User);

                array = (UserDB[])memcacheFacade[key];

                var item = (from r in array
                            where r.Id.Pid == User.Id.Pid
                            select r).FirstOrDefault();

                if (item != null)
                {
                    List<UserDB> list = new List<UserDB>();
                    list.AddRange(array);
                    list.Remove(item);
                    list.Add(User);

                    memcacheFacade.Set(key, list.ToArray(), DateTime.Now.AddDays(2));
                }
            }
            else
            {
                us.Update(User);
                memcacheFacade.Add(key, us.GetItems().ToArray(), DateTime.Now.AddDays(2));
            }

            return true;
        }

        public bool DeleteUser(short id)
        {
            UserDB[] array = null;

            MemcacheFacade memcacheFacade = new MemcacheFacade();
            string key = "UserDB";
            UserService us = new UserService();
            if (memcacheFacade.Contains(key))
            {
                us.Delete(id);

                array = (UserDB[])memcacheFacade[key];

                var item = (from r in array
                            where r.Id.Pid == id
                            select r).FirstOrDefault();

                if (item != null)
                {
                    List<UserDB> list = new List<UserDB>();
                    list.AddRange(array);
                    list.Remove(item);

                    memcacheFacade.Set(key, list.ToArray(), DateTime.Now.AddDays(2));
                }
            }
            else
            {
                us.Delete(id);
                memcacheFacade.Add(key, us.GetItems().ToArray(), DateTime.Now.AddDays(2));
            }

            return true;
        }
    }
}
